// EG4Data.h -- data structure for holding sim data
// Created: Sept. 24, 2024 by Asia SUN, asia.rabbit@163.com
// Last modified: Sept. 24, 2024, by Asia SUN

#include "EG4Data.h"
#include "G4SystemOfUnits.hh"

EG4Data::EG4Data(){
  hit_t n; n.initialize();
  for(int i = 2; i--;) hTOFW.push_back(n);
  for(int i = 8; i--;) hDCTaD.push_back(n); // 2*4 = [DC0-1][x0-1--y0-1]
  for(int i = 18; i--;) hDC.push_back(n); // 3*6 = [DC0-1-2][x0-1--u0-1--v0-1]
  for(int i = 2; i--;) hNWS.push_back(n);
  for(int i = 10; i--;) hNWF.push_back(n);
} // end ctor

void EG4Data::Initialize(){
  index = -1; a = z = -999.;
  pp0 = theta = phi = tof = -999.;
  for(int i = 3; i--;) p0[i] = -999.;
  nhT1 = nhTOFW = nhDCTaD = nhDC = nhNWS = nhNWF = nhNWV = 0;

  hT1.initialize(); hNWV.initialize();
  hTOFW[0].initialize(); hTOFW[1].initialize();
  for(auto &t : hDCTaD) t.initialize();
  for(auto &t : hDC) t.initialize();
  for(auto &t : hNWS) t.initialize();
  for(auto &t : hNWF) t.initialize();
} // end member function initialize

void EG4Data::copy(double *pp, const G4ThreeVector &p){
  pp[0] = p[0]; pp[1] = p[1]; pp[2] = p[2];
} // end member function copy

void EG4Data::PostAssign(){
  pp0 = sqrt(p0[0]*p0[0]+p0[1]*p0[1]+p0[2]*p0[2]);
  theta = atan(sqrt(p0[0]*p0[0]+p0[1]*p0[1]) / p0[2]) / deg;
  if(theta < 0) theta += 180.;

  phi = fabs(atan(p0[1]/p0[0])) / deg;
  if(p0[1] >= 0.){
    if(0. == p0[0]) phi = 90.;
    else if(p0[0] < 0.) phi = 180. - phi;
  } // end if
  else if(p0[1] < 0.){
    if(0. == p0[0]) phi = 270.;
    else if(p0[0] < 0.) phi = 180. + phi;
    else phi = 360. - phi;
  } // end else if
  if(phi > 270.) phi = phi - 360.;

  if(nhT1 && nhTOFW) tof = hTOFW[0].t - hT1.t;
} // end member function PostAssign

double EG4Data::pphi() const{
  if(phi > 270.) return phi - 360.;
  else return phi;
} // end member function pphi
